<%@page import="helpers.StringHelper"%>
<%@page import="helpers.UrlHelper"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory"%>
<%@page import="com.google.appengine.api.blobstore.BlobstoreService"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="java.util.List"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="helpers.DateHelper"%>
<%@page import="entities.User"%>
<%@page import="entities.Category"%>
<%@page import="entities.Article"%>
<%
Article article = (Article) request.getAttribute("article");
List<Article> cArticles = (List<Article>) request.getAttribute("cArticles");
List<Article> uArticles = (List<Article>) request.getAttribute("uArticles");
Category category = (Category) request.getAttribute("category");
User author = (User) request.getAttribute("author");
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
ImagesService imagesService = ImagesServiceFactory.getImagesService();

//Set content for seo
String nameServer = UrlHelper.getDomainServer(request);
String title = article.getTitle() + " - " + nameServer;
String description = StringHelper.cutString(StringHelper.html2text(article.getContent()), 50);
String keywords = article.getTags() + ", archives, archives article, submit article, article commit";
String url = UrlHelper.getServerUrl(request) + "/a/" + article.getAlias();
%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="/layouts/header.jsp">
	<jsp:param name="title" value="<%=title %>" />
    <jsp:param name="description" value="<%=description %>" />
    <jsp:param name="keywords" value="<%=keywords %>" />
    <jsp:param name="ogtitle" value="<%=title %>" />
    <jsp:param name="ogurl" value="<%=url %>" />
    <jsp:param name="ogdescription" value="<%=description%>" />
</jsp:include>

            <div id="mainContent" class="left" itemscope itemtype="http://schema.org/ItemList">
                <div id="detailPage">
                    <article class="articleItem" itemscope itemtype="http://schema.org/Article">
                        <h1 class="articleTitle" itemprop="name"><%=article.getTitle() %></h1>
                        <div class="articleMeta clearafter">
                        	<% if (author != null) { %>
                            by <a href="/u/<%=author.getId() %>" itemprop="author"><%=author.getName() %></a> | 
                            <% } else { %>
                            by <%=article.getAuthorName() %> |
                            <% } %>  
							on <span itemprop="dateCreated"><%=DateHelper.getStringFromDate(article.getCreated(), "MMM dd, yyyy") %></span>  
							<% if (category != null) { %>
                            <a href="/c/<%=category.getAlias() %>" class="articleCat right"><%=category.getName() %></a>
                            <% } %>
                            <meta itemprop="interactionCount" content="UserComments:5"/>
                        </div>
                        <div class="entry clearafter" itemprop="articleBody">
                        	<details open>
                        		<div class="adv">
                            		<script type="text/javascript"><!--
									google_ad_client = "ca-pub-2883566427648799";
									/* link_menu 468x15 */
									google_ad_slot = "8735411486";
									google_ad_width = 468;
									google_ad_height = 15;
									//-->
									</script>
									<script type="text/javascript"
									src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
									</script>
                            	</div>
                            	<div class="adv-2">
                            		<script type="text/javascript"><!--
									google_ad_client = "ca-pub-2883566427648799";
									/* 300x250 */
									google_ad_slot = "2688877886";
									google_ad_width = 300;
									google_ad_height = 250;
									//-->
									</script>
									<script type="text/javascript"
									src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
									</script>
                            	</div>
                                <%=article.getContent() %>
                            </details>
                        </div>
                    </article>
                    
                    <% if (author != null) { %>
                    <section class="authorInfo clearafter" itemscope itemtype="http://schema.org/Person">
                    	<div class="left authorAvatar">
                        	<a href="/u/<%=author.getId() %>">
                        		<% 
									if (author.getAvatar() != null && !author.getAvatar().isEmpty())  {
										 BlobKey blobKey = new BlobKey(author.getAvatar());
								%>
								<img alt="" src="<%=imagesService.getServingUrl(blobKey) %>=s120-c"><br />
								<% } else { %>							
								<img class="img-full-width" itemprop="image" src="/images/icon-author.jpg" />
								<% } %>
                        	</a>
                        </div>
                        <h2 itemprop="name"><a href="/u/<%=author.getId() %>"><%=author.getName() %></a></h2>
                        <a href="<%=author.getWebsite() %>" itemprop="url"><%=author.getWebsite() %></a>
                        <p class="intro" itemprop="description"><%=author.getAboutme() %></p>
                    </section>
                    <% } %>
                    
                    <%  if (uArticles != null && uArticles.size() > 0) { %>
                    <section class="authorArticles">
                    	<h3 class="title">Author's articles</h3>
                    	<ul class="articleListing" itemscope itemtype="http://schema.org/ItemList">
                    		<%
                    		for (int i = 0; i < uArticles.size(); i++) {
                    			if (uArticles.get(i).getId().equals(article.getId())) break;
                    		%>
                            <li itemscope itemtype="http://schema.org/Article" itemprop="itemListElement">
                            	<h3 itemprop="name"><a href="/a/<%=uArticles.get(i).getAlias() %>"><%=uArticles.get(i).getTitle() %></a></h3>
                            </li>
                            <% } %>
                        </ul>
                    </section>
                    <% } %>
                    
                    <% if (article.getKeywords().size() > 0) { %>
                    <section class="tagListing" itemscope itemtype="http://schema.org/CreativeWork">
                    	<h3 class="title">Tags</h3>
                    	<% for (int i = 0; i < article.getKeywords().size(); i++) { %>
                    	<%= i > 0 ? ", " : "" %>
                    	<a href="/k/<%=URLEncoder.encode(article.getKeywords().get(i)) %>" itemprop="keywords"><%=article.getKeywords().get(i) %></a> 
                    	<% } %>
                    </section>
                    <% } %>
                    
                    <% if (cArticles != null && cArticles.size() > 0) { %>
                    <section class="otherArticles">
                    	<h3 class="title">Other articles</h3>
                    	<ul class="articleListing" itemscope itemtype="http://schema.org/ItemList">
                    		<% 
                    		for (int i = 0; i < cArticles.size(); i++) {
                    			if (cArticles.get(i).getId().equals(article.getId())) break;
                    		%>
                            <li itemscope itemtype="http://schema.org/Article" itemprop="itemListElement">
                            	<h3 itemprop="name"><a href="/a/<%=cArticles.get(i).getAlias()%>"><%=cArticles.get(i).getTitle()%></a></h3>
                            </li>
                            <% } %>
                        </ul>
                    </section>
                    <% } %>
                    
                </div>
            </div>
            
            <jsp:include page="/elements/right.jsp" />
            
            <div class="clearbreak"></div>
<jsp:include page="/layouts/footer.jsp" />